//
//  ASListController.swift
//  soapp
//
//  Created by tong on 2017/7/18.
//

import UIKit


import UIKit
import Kingfisher
import Eelay
import JoLoading
import AsyncDisplayKit


open class ASListController: TypeInitController,ASTableVConfig,PageContainerOffset {
    
    public var tableNode:ASTableNode = ASTableNode(style: .plain)
    public var tableView:UITableView{
        get{
            return tableNode.view
        }
    }
    
    public var so_constrains = [NSLayoutConstraint]()
    public var edegs:(top:CGFloat,left:CGFloat,bottom:CGFloat,right:CGFloat) = (0,0,0,0)
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    required public init() {
        super.init()
    }
    
    public var page_load_section = "section0"

    public var sectionIndex:Int{
        get{
            let s = self.page_load_section.replacingOccurrences(of: "section", with: "")
            return Int(s) ?? 0
        }
    }
   
    public func load(sections:TP.section,selector:TP.selector)
    {
        tableNode.load(sections: sections, selector: selector)
    }
    
    public var contentInsetView: UIScrollView? {
        get{
            return self.tableView
        }
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor._background
        
    
        tableView.separatorStyle = .none
        layTable(tableView: tableView)
        
        
        tableView.hidenMoreLine()
        
        self.configTables(nodes: tableNode)
        
    }
    
    open func layTable(tableView:UIView)
    {        
        jo_contentView.eelay = [
            [tableView,[ee.T.L.B.R]]
        ]
    }
    
    
    public func remove_constains()
    {
        self.jo_contentView.removeConstraints(self.so_constrains)
    }
    
    open func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        TypeInitController.IDActiveAction(self,actionID,model)
    }
    
    
    open func reloadDataActionHappensHere(){
        let count = tableNode.dataSource?.tableNode?(tableNode, numberOfRowsInSection: 0) ?? 0
        if count > 2 {
            for row in 0...(count-1)
            {
                indexPathesToBeReloaded.append(IndexPath(row:row,section:0))
            }
        }
        self.tableNode.reloadData()
        tableNode.waitUntilAllUpdatesAreProcessed();
        
    }
    public var indexPathesToBeReloaded: [IndexPath] = []

    
    open func checkCell(cell:ASCellNode,indexPath: IndexPath) {
        
    }
    
    open func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
        
        if let cell = tableNode.nodeForRow(at: indexPath) as? JoCellNode{
            cell.didSelect(at: indexPath)
        }
        
//        let node = tableNode.cellNode(indexPath: indexPath)
        
    }
    
    
    open func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode {
        let cell = tableNode.cellNode(indexPath: indexPath, fillModel: true, delegate: self)
        cell.neverShowPlaceholders = true
        if let cnode = cell as? JoCellNode
        {
            cnode.mo.controller = self
            cnode.delegate = self
        }
        checkCell(cell: cell, indexPath: indexPath)
        if indexPathesToBeReloaded.contains(indexPath) {
            let oldCellNode = tableNode.nodeForRow(at: indexPath)
            cell.neverShowPlaceholders = true
            oldCellNode?.neverShowPlaceholders = true
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                cell.neverShowPlaceholders = false
                if let indexP = self.indexPathesToBeReloaded.firstIndex(of: indexPath) {
                    self.indexPathesToBeReloaded.remove(at: indexP)
                }
            })
        }
        return cell
    }
    
    

    
    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {

        
        KingfisherManager.shared.cache.clearMemoryCache()

    }
 
    
    
}

